/*
 * Project: BooEditor
 * Author:  Johannes Kuhaupt
 * Copyright (c): Johannes Kuhaupt
 * Licence: MIT
 */
package view;

import BooSyntax.TabBreakAction;
import control.GuiManager;
import control.SettingsManager;
import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.IOException;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import org.fife.ui.rsyntaxtextarea.FileLocation;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.fife.ui.rsyntaxtextarea.Theme;
import org.fife.ui.rtextarea.RTextScrollPane;
import org.fife.ui.rtextarea.SearchContext;
import org.fife.ui.rtextarea.SearchEngine;

/**
 *
 * @author Johannes Kuhaupt
 */
public class BooTextEditorPane extends org.fife.ui.rsyntaxtextarea.TextEditorPane{
    
    JPopupMenu popup;
    RTextScrollPane derScroller;
    Color colorLineHighlighting;
    java.util.ResourceBundle langBundle = GuiManager.getLangBundle();
    
    /**
     *
     */
    public BooTextEditorPane(){
        super();
        this.setEOLMarkersVisible(false);
        this.setSyntaxEditingStyle("text/boo");
        this.getActionMap().put("insert-break", new TabBreakAction());
        this.setEncoding("UTF8");
    }
    
      
    public void setIndentation(int pSize, boolean pTabsAsSpaces){
        this.getActionMap().put("insert-break", new TabBreakAction(pSize, pTabsAsSpaces));
    }
    
    /**
     *
     * @param pScroll
     */
    public void setScroller(RTextScrollPane pScroll){
        this.derScroller = pScroll;
    }
    
    
    /**
     *
     */
    public void customizeContextMenu(){
            popup = this.getPopupMenu();
            JMenuItem popMark = new JMenuItem();
            popMark.setText(langBundle.getString("HighlightSelection"));
        
        JMenuItem popClearMark = new JMenuItem();
        popClearMark.setText(langBundle.getString("ClearHighlighting"));

        
        
        popClearMark.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                popupClearMarksActionPerformed(evt);
            }
        });
        
        
        popMark.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                popupMarkActionPerformed(evt);
            }
        });
        
        //popup.remove(6);
        popup.remove(10);
        popup.add(popMark);
        popup.add(popClearMark);
        JMenuItem narf = (JMenuItem) popup.getComponent(0);
        narf.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/arrow_undo.png")));
        narf = (JMenuItem) popup.getComponent(1);
        narf.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/arrow_redo.png")));
        narf = (JMenuItem) popup.getComponent(3);
        narf.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/cut.png")));
        narf = (JMenuItem) popup.getComponent(4);
        narf.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/page_copy.png")));
        narf = (JMenuItem) popup.getComponent(5);
        narf.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/page_paste.png")));
        narf = (JMenuItem) popup.getComponent(6); 
        narf.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/delete.png")));
        narf = (JMenuItem) popup.getComponent(8); 
        narf.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/text_align_left.png")));
        narf = (JMenuItem) popup.getComponent(10); 
        narf.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/lightning_add.png")));
        narf = (JMenuItem) popup.getComponent(11); 
        narf.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/lightning_delete.png")));
        
    }
    
    /**
     *
     */
    public void setStandardStyle(){
        this.derScroller.setLineNumbersEnabled(true);
        this.derScroller.getGutter().setLineNumberFont(new Font(SettingsManager.getStringSettingValue("FONTNAME"),SettingsManager.getIntSettingValue("FONTSTYLE"), SettingsManager.getIntSettingValue("FONTSIZE") ));
        
        this.setEncoding("UTF8");
        this.setText("");
        this.setCodeFoldingEnabled(false);
        
        this.setAntiAliasingEnabled(true);
        this.setAutoIndentEnabled(true);
        
        this.setAnimateBracketMatching(false);
        this.setBracketMatchingEnabled(false);
        this.setLineWrap(false);
        this.setSyntaxEditingStyle("text/boo");
        this.discardAllEdits();
        
        this.customizeContextMenu();
    }
    
    private void changeStyleViaThemeXml(String pTheme) {
      try {
         Theme theme = Theme.load(getClass().getResourceAsStream("/HighlightThemes/"+pTheme+".xml"));
         theme.apply(this);
      } catch (IOException ioe) { // Never happens
         ioe.printStackTrace();
      }
   }
    
    /**
     *
     */
    public void updateOptions(){
        this.derScroller.setLineNumbersEnabled(SettingsManager.getBooleanSettingValue("LINENUMBERS"));
        this.setCodeFoldingEnabled(false);
        this.doBracketMatching();        
        this.setAntiAliasingEnabled(SettingsManager.getBooleanSettingValue("ANTIALIAS"));
        this.setAutoIndentEnabled(SettingsManager.getBooleanSettingValue("AUTOINDENTATION"));
        
        //this.setAnimateBracketMatching(SettingsManager.getBooleanSettingValue("HIGHLIGHTBRACES"));
        this.setWhitespaceVisible(SettingsManager.getBooleanSettingValue("SHOWSPACES"));
        
        this.setHighlightCurrentLine(SettingsManager.getBooleanSettingValue("HIGHLIGHTACTIVELINE"));
        this.setTabSize(SettingsManager.getIntSettingValue("TABSIZE"));
        
        //this.setBracketMatchingEnabled(SettingsManager.getBooleanSettingValue("AUTOBRACES"));
        //this.doBracketMatching();
        this.changeStyleViaThemeXml(SettingsManager.getStringSettingValue("HIGHLIGHTINGTHEME"));         
        
        if(SettingsManager.getBooleanSettingValue("SYNTAXHIGHLIGHTING")){
            //this.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_PYTHON);
            this.setSyntaxEditingStyle("text/boo");
        }
        else{
            this.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_NONE);
        }
        String fName = SettingsManager.getStringSettingValue("FONTNAME");
        int fStyle = SettingsManager.getIntSettingValue("FONTSTYLE");
        int fSize;
        if(GuiManager.bigScreenModus()){
            fSize = SettingsManager.getIntSettingValue("FONTSIZEBIGSCREEN");
        }
        else{
            fSize = SettingsManager.getIntSettingValue("FONTSIZE");
        }
        this.setFont(new Font(fName,fStyle,fSize ) ); 
        
        
        
        if(SettingsManager.getBooleanSettingValue("INDENTATIONBYTAB")){
            this.setIndentation(SettingsManager.getIntSettingValue("TABSIZE"), false);
            this.setTabsEmulated(false);
            if(this.getDocument().getLength()>0){
                this.convertSpacesToTabs();
            }
            
        }
        else{
            this.setIndentation(SettingsManager.getIntSettingValue("TABSIZE"), true);
            this.setTabsEmulated(true);
            if(this.getDocument().getLength()>0){
                this.convertTabsToSpaces();
            }
        }
    }
    
    /**
     *
     * @param pWord
     * @param pForward
     * @return
     */
    public boolean search(String pWord, boolean pForward){
        SearchContext context = new SearchContext();
        
        
        context.setSearchFor(pWord);
        context.setMatchCase(false);
        context.setRegularExpression(false);
        context.setSearchForward(pForward);
        context.setWholeWord(false);
        
        return SearchEngine.find(this, context);
    }
    
        
    private void popupMarkActionPerformed(java.awt.event.ActionEvent evt) {                                         
        this.markAll(this.getSelectedText(), false, false,false);
    }
    
     private void popupClearMarksActionPerformed(java.awt.event.ActionEvent evt) {                                         
        this.clearMarkAllHighlights();
    }
     
    public void loadAndShowFile(File pFile) throws IOException{
            this.load(FileLocation.create(pFile), "UTF8");        
    }
    
}
